package com.chenqi.community.handler;

import com.chenqi.community.exception.NotFoundException;
import com.chenqi.community.exception.OverSizeException;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Ardai
 * 异常拦截器
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Exception.class)
    public ModelAndView exception(HttpServletRequest request,Exception e) throws Exception {
        logger.error("RequestURL:{} ,Exception {}", request.getRequestURL(), e.getMessage());
        //对于资源未找到的处理
        if (e instanceof NotFoundException) {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("url", request.getRequestURL());
            modelAndView.addObject("exception", e);
            modelAndView.setViewName("404");
            return modelAndView;
        }
        //服务器内部异常处理
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("url", request.getRequestURL());
        modelAndView.addObject("exception", e);
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
